package com.itrip.biz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itrip.beans.pojo.HotelRoom;
import com.itrip.beans.pojo.LabelDic;
import com.itrip.beans.vo.HotelRoomVo;
import com.itrip.biz.service.HotelRoomService;
import com.itrip.biz.service.ImageService;
import com.itrip.biz.service.LabelDicService;
import com.itrip.utils.dto.Dto;
import com.itrip.utils.util.DateUtil;
import com.itrip.utils.util.EmptyUtils;
import com.itrip.utils.vo.DtoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping(value = "/biz/api/hotelroom")
public class HotelRoomController {
    @Autowired
    private HotelRoomService hotelRoomService;
    @Autowired
    private LabelDicService labelDicService;
    @Autowired
    private ImageService imageService;

    //根据酒店ID查询 酒店房型列表
    @PostMapping("/queryhotelroombyhotel")
    private Dto<HotelRoom> queryHotelRoomByhotelId(@RequestBody HotelRoomVo vo) {
        List<List<HotelRoom>> hotelRoomVOList = null;
        try {
            Map<String, Object> param = new HashMap();
            if (EmptyUtils.isEmpty(vo.getHotelId())) {
                return DtoUtil.returnFail("酒店ID不能为空");
            }
            if (EmptyUtils.isEmpty(vo.getStartDate()) || EmptyUtils.isEmpty(vo.getEndDate())) {
                return DtoUtil.returnFail("必须填写酒店入住及退房时间");
            }
            if (EmptyUtils.isNotEmpty(vo.getStartDate()) && EmptyUtils.isNotEmpty(vo.getEndDate())) {
                if (vo.getStartDate().getTime() > vo.getEndDate().getTime()) {
                    return DtoUtil.returnFail("入住时间不能大于退房时间");
                }
                List<Date> dates = DateUtil.getBetweenDates(vo.getStartDate(), vo.getEndDate());
                param.put("timesList", dates);
            }
            if (vo.getHotelId()!=null){
                param.put("hotelId", vo.getHotelId());
            }
            if (vo.getIsBook()!=null){
                param.put("isBook", vo.getIsBook());
            }
            if (vo.getIsHavingBreakfast()!=null){
                param.put("isHavingBreakfast", vo.getIsHavingBreakfast());
            }
            if (vo.getIsTimelyResponse()!=null){
                param.put("isTimelyResponse", vo.getIsTimelyResponse());
            }
            if (vo.getRoomBedTypeId()!=null){
                param.put("roomBedTypeId", vo.getRoomBedTypeId());
            }
            if (vo.getIsCancel()!=null){
                param.put("isCancel", vo.getIsCancel());
            }
            if (vo.getPayType()!=null){
                param.put("payType", vo.getPayType());
            }

            List<HotelRoom> originalRoomList = hotelRoomService.getHotelRoomListByMap(param);
            hotelRoomVOList = new ArrayList();
            for (HotelRoom roomVO : originalRoomList) {
                List<HotelRoom> tempList = new ArrayList<HotelRoom>();
                tempList.add(roomVO);
                hotelRoomVOList.add(tempList);
            }
            return DtoUtil.returnSuccess("获取成功", hotelRoomVOList);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("获取酒店房型列表失败");
        }
    }

    //查询酒店房间 床型
    @GetMapping("/queryhotelroombed")
    public Dto<LabelDic> getHotelRoomBed(){
        QueryWrapper qw=new QueryWrapper();
        qw.eq("parent_id",1);
        List<LabelDic> labelDics=labelDicService.list(qw);
        return DtoUtil.returnDataSuccess(labelDics);
    }


    //根据targetId查询酒店房型图片(type=1)
    @GetMapping("/getimg/{targetId}")
    public Dto getImg(@PathVariable String targetId){
        QueryWrapper qw=new QueryWrapper();
        qw.eq("type",1);
        qw.eq("target_id",targetId);
        return DtoUtil.returnDataSuccess(imageService.list(qw));
    }

}
